﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Slf.Samples.CustomFactoryImplementation
{
  public class MyFactory : ILoggerFactory
  {
    //THIS SAMPLE FACTORY IMPLEMENTS ILOGGERFACTORY DIRECTLY FOR SIMPLICITY.
    //IN ORDER TO SUPPORT NAMED LOGGERS, DERIVING FROM NamedLoggerFactoryBase IS RECOMMENDED

    private ILogger testLogger;
    private ILogger defaultLogger;

    public MyFactory()
    {
      defaultLogger = new ColoredConsoleLogger();

      //for the test logger, write log messages to console and debug output
      testLogger = new CompositeLogger(new ConsoleLogger(), new DebugLogger());
    }

    /// <summary>
    /// Obtains an <see cref="ILogger"/> instance that is identified by
    /// the given name.
    /// </summary>
    /// <param name="name">The logger name. Submit <see cref="LoggerService.DefaultLoggerName"/>
    /// in order to request a default (unnamed) logger.</param>
    /// <returns>An <see cref="ILogger"/> instance.</returns>
    public ILogger GetLogger(string name)
    {
      switch(name)
      {
        case "Test":
          return testLogger;
        case LoggerService.DefaultLoggerName:
          return defaultLogger;
        default:
          //in case of an unknown name, write to console, prefixed with requested logger name
          return new DelegateLogger(li => Console.WriteLine("[{0}] {1}", name, li.Message));
      }
    }
  }
}
